package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.RestMapper;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Rest;
import com.itheima.pojo.Rest;
import com.itheima.pojo.User;
import com.itheima.service.RestService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class RestServiceImpl implements RestService {

    @Autowired
    private RestMapper restMapper;
    @Autowired
    private UserMapper userMapper;


    @Override
    public void add(Rest rest) {
        Rest restdetail=new Rest();
        BeanUtils.copyProperties(rest, restdetail);
        restdetail.setCreateTime(LocalDateTime.now());
        System.out.println(restdetail.getReason()+"service");
        System.out.println(restdetail.getDays());
        System.out.println(restdetail.getCreateTime()+"service");
        restMapper.insert(restdetail);
    }

    @Override
    public void update(Rest rest) {
        rest.setNickname(null);
        rest.setSearch(null);
        rest.setUpdateTime(LocalDateTime.now());
        restMapper.updateById(rest);
    }

    @Override
    public PageBean<Rest> list(Integer pageNum, Integer pageSize, String nickname, String isadmin, String search, Integer userId) {
        PageBean<Rest> pb = new PageBean<>();

        // 开启分页查询
        PageHelper.startPage(pageNum, pageSize);

        // 使用自定义 SQL 查询
        List<Rest> as = restMapper.selectWithLeftJoin(isadmin, nickname, search, userId);

        System.out.println(as + "集合");

        // 填充 PageBean
        Page<Rest> page = (Page<Rest>) as;
        pb.setTotal(page.getTotal());
        pb.setItems(page.getResult());
        return pb;
    }



}
